package com.common.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;

public class BottomView {
    private View convertView;
    private Context context;
    private int theme;
    private Dialog bv;
    private int animationStyle;
    private boolean isTop = false;

    public BottomView(Context c, int theme, View convertView) {
        this.theme = theme;
        this.context = c;
        this.convertView = convertView;
    }

    public BottomView(Context c, int theme, int resource) {
        this.theme = theme;
        this.context = c;
        this.convertView = View.inflate(c, resource, (ViewGroup) null);
    }

    public void showBottomView(boolean CanceledOnTouchOutside) {
        if (this.theme == 0) {
            this.bv = new Dialog(this.context);
        } else {
            this.bv = new Dialog(this.context, this.theme);
        }
        this.bv.setCanceledOnTouchOutside(CanceledOnTouchOutside);
        this.bv.getWindow().requestFeature(1);

        this.bv.setContentView(this.convertView);
        Window wm = this.bv.getWindow();
        WindowManager m = wm.getWindowManager();
        Display d = m.getDefaultDisplay();
        LayoutParams p = wm.getAttributes();
        p.width = d.getWidth() * 1;
        if (this.isTop) {
            p.gravity = 48;
        } else {
            p.gravity = 80;
        }

        if (this.animationStyle != 0) {
            wm.setWindowAnimations(this.animationStyle);
        }

        wm.setAttributes(p);
        this.bv.show();
    }


    public void setTopIfNecessary() {
        this.isTop = true;
    }

    public void setAnimation(int animationStyle) {
        this.animationStyle = animationStyle;
    }

    public View getView() {
        return this.convertView;
    }

    public void dismissBottomView() {
        if (this.bv != null) {
            this.bv.dismiss();
        }

    }


}
